package com.framework.modules.base.service.impl;

import com.framework.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.UserPolicySettingDao;
import com.framework.modules.base.entity.UserPolicySettingEntity;
import com.framework.modules.base.service.UserPolicySettingService;


@Service("userPolicySettingService")
public class UserPolicySettingServiceImpl extends ServiceImpl<UserPolicySettingDao, UserPolicySettingEntity> implements UserPolicySettingService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserPolicySettingEntity> page = this.selectPage(
                new Query<UserPolicySettingEntity>(params).getPage(),
                new EntityWrapper<UserPolicySettingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Object saveUserPoliy(UserPolicySettingEntity entity) {
        UserPolicySettingEntity userPolicySettingEntity=baseMapper.selectById(entity.getUid());
        if(userPolicySettingEntity==null){
            entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            return baseMapper.insert(entity);
        }
        if(StringUtils.isNotBlank(entity.getAttentSetting())){
            userPolicySettingEntity.setAttentSetting(entity.getAttentSetting());
        }
        if(StringUtils.isNotBlank(entity.getChatSetting())){
            userPolicySettingEntity.setChatSetting(entity.getChatSetting());
        }
        if(StringUtils.isNotBlank(entity.getAttentSetting())){
            userPolicySettingEntity.setPostSetting(entity.getPostSetting());
        }
        if(StringUtils.isNotBlank(entity.getSenceSetting())){
            userPolicySettingEntity.setSenceSetting(entity.getSenceSetting());
        }
        userPolicySettingEntity.setLastEdit(DateUtils.getSecondTimestamp(new Date()));
        return baseMapper.updateById(userPolicySettingEntity);
    }

    @Override
    public Object findUserPoliy(Long uid) {
        UserPolicySettingEntity userPolicySettingEntity=baseMapper.selectById(uid);
        if(userPolicySettingEntity==null){
            userPolicySettingEntity=new UserPolicySettingEntity();
            userPolicySettingEntity.setPostSetting("all");
            userPolicySettingEntity.setSenceSetting("1");
            userPolicySettingEntity.setChatSetting("1");
            userPolicySettingEntity.setAttentSetting("1");
            userPolicySettingEntity.setUid(uid.intValue());
            userPolicySettingEntity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            baseMapper.insert(userPolicySettingEntity);
        }
        return userPolicySettingEntity;
    }

}
